import 'package:flutter/material.dart';

class NormalInput extends StatelessWidget {
  final TextEditingController controller;
  final Icon? icon;
  final Icon? suffixIcon;
  final String? title;
  final double? height;
  final double radius;
  final void Function(String)? onSubmitted;
  final bool showClear;

  const NormalInput({
    super.key,
    required this.controller,
    this.icon,
    this.suffixIcon,
    this.title = '',
    this.height = 35,
    this.onSubmitted,
    this.showClear = true,
    this.radius = 20,
  });

  OutlineInputBorder _buildCustomBorder() {
    return OutlineInputBorder(
      borderRadius: BorderRadius.circular(radius),
      borderSide: BorderSide.none,
    );
  }

  InputDecoration _buildInputDecoration(BuildContext context) {
    final theme = Theme.of(context).colorScheme;
    return InputDecoration(
      hintText: '请输入关键词',
      hintStyle: TextStyle(color: Colors.grey[500]),
      prefixIcon: icon,
      fillColor: theme.primaryContainer,
      filled: true,
      border: _buildCustomBorder(),
      focusedBorder: OutlineInputBorder(
        borderRadius: BorderRadius.circular(radius),
        borderSide: BorderSide(
          color: theme.primary,
          width: 1.2,
        ),
      ),
      focusColor: theme.primary,
      contentPadding: EdgeInsets.symmetric(
        vertical: height! * 0.3,
        horizontal: 16,
      ),
      suffixIcon: _buildSearchButton(),
    );
  }

  Widget _buildSearchButton() {
    return IconButton(
      icon: suffixIcon ?? const Icon(Icons.search, size: 20),
      onPressed: () => onSubmitted?.call(controller.text),
    );
  }

  Widget _buildTextFormField(BuildContext context) {
    return TextFormField(
      controller: controller,
      style: const TextStyle(
        fontWeight: FontWeight.w400,
        height: 1,
      ),
      // 设置聚焦时的样式
      cursorColor: Theme.of(context).colorScheme.primary,
      // 设置输入框的样式
      decoration: _buildInputDecoration(context),
      onFieldSubmitted: onSubmitted,
    );
  }

  @override
  Widget build(BuildContext context) {
    return Card(
      elevation: 4,
      shape: RoundedRectangleBorder(
        borderRadius: BorderRadius.circular(radius),
      ),
      child: SizedBox(
        width: 100,
        height: height,
        child: ValueListenableBuilder<TextEditingValue>(
          valueListenable: controller,
          builder: (context, _, __) => _buildTextFormField(context),
        ),
      ),
    );
  }
}
